package com.one.consumer.order.ui.binder;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.bean.PayWXBean;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.utils.PayUtils;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.common.view.widget.InputLayout;
import com.one.consumer.R;
import com.one.consumer.common.state.OrderStateCu;
import com.one.consumer.order.model.OrderModel;
import com.one.consumer.reserve.model.item.CustomItem;

/**
 * Author: zhulunjun
 * Time  : 2018/10/26 9:51
 * email : zhulunjun@qq.com
 * Describe : 装修订单
 **/
public class CustomOrderBinder extends BaseItemBinder<CustomItem> {
    public CustomOrderBinder() {
        super(R.layout.item_order_de);
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull CustomItem item) {
        InputLayout ilTime = holder.getView(R.id.il_time);
        InputLayout ilType = holder.getView(R.id.il_type);
        InputLayout ilContact = holder.getView(R.id.il_contact);
        InputLayout ilPhone = holder.getView(R.id.il_phone);
        InputLayout ilAddress = holder.getView(R.id.il_address);
        ilType.setName("项目名称");
        ilTime.setText(item.getCreate_time());
        ilType.setText(item.getCustomized_name());
        ilContact.setText(item.getContact_name());
        ilPhone.setText(item.getContact_mobile());
        ilAddress.setText(item.getAddress_detail());


        TextView btn1 = holder.getView(R.id.tv_btn_1);
        TextView btn2 = holder.getView(R.id.tv_btn_3);

        btn1.setText("查看详情");
        switch (item.getOrder_state()) {
            case OrderStateCu.CONFIRM:
                btn2.setText("支付定金");
                break;
            case OrderStateCu.PAY_1:
                btn2.setText("支付尾款");
                break;
            case OrderStateCu.PAY_2:
            case OrderStateCu.END:
                btn2.setText("已完成");
                btn2.setBackgroundResource(R.drawable.shape_bg_red_r2);
                btn2.setTextColor(mContext.getResources().getColor(R.color.text_color_black_33));
                break;
            default:
                btn2.setVisibility(View.GONE);
                break;

        }

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RouterManager.getInstance().go(RouterPath.CU_ORDER_DETAIL,
                        new DefaultExtra(item.getOrder_id()+""));
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取支付凭证，跳转微信支付
                if(item.getOrder_state().equals(OrderStateCu.CONFIRM) ||
                        item.getOrder_state().equals(OrderStateCu.PAY_1)) {
                    pay(item.getOrder_id() + "");
                }
            }
        });
    }

    /**
     * 调起微信支付
     * @param orderId
     */
    private void pay(String orderId){
        new OrderModel((BaseActivity) mContext).getPayConfigCU(orderId, new ObserverOnResultListener<PayWXBean>() {
            @Override
            public void onResult(PayWXBean o) {
                PayUtils.getInstance(mContext).wxPay(o);
            }
        });
    }

}
